package com.css.fxfzxczh.modules.resultsManagement.service.impl;

import com.css.fxfzxczh.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzxczh.base.attachment.service.AttachmentInfoService;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.resultsManagement.entity.MapManagementEntity;
import com.css.fxfzxczh.modules.resultsManagement.repository.MapManagementRepository;
import com.css.fxfzxczh.modules.resultsManagement.service.MapManagementService;
import com.css.fxfzxczh.util.*;
import com.css.fxfzxczh.zcpt.sys.entity.SUser;
import com.css.fxfzxczh.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

@Service
public class MapManagementServiceImpl implements MapManagementService {
    @Resource
    MapManagementRepository mapManagementRepository;
    @Resource
    SUserService sUserService;
    @Resource
    MapManagementService mapManagementService;
    @Resource
    AttachmentInfoService attachmentInfoService;

    @Override
    public String saveMap(MapManagementEntity mapManagementEntity) {
        MapManagementEntity managementEntity = mapManagementRepository.getByLabel(mapManagementEntity.getXsemId(), mapManagementEntity.getMapLabel());
        if (managementEntity != null) {
            mapManagementEntity.setMapId(managementEntity.getMapId());
            mapManagementEntity.setCreateUser(managementEntity.getCreateUser());
            mapManagementEntity.setCreateTime(managementEntity.getCreateTime());
            MapManagementEntity entity = mapManagementService.update(mapManagementEntity);
            return entity.getMapId();
        }
        String mapId = UUIDGenerator.getUUID();
        mapManagementEntity.setMapId(mapId);
        mapManagementEntity.setDelFlag(YNEnum.N.toString());
        mapManagementEntity.setCreateUser(PlatformSessionUtils.getUserId());
        if (mapManagementEntity.getUploader() == null) {
            String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
            SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
            if (sUser != null) {
                mapManagementEntity.setUploader(sUser.getUserName());
            }
        }
        mapManagementEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        mapManagementEntity.setUploadTime(PlatformDateUtils.getCurrentTimestamp());
        mapManagementRepository.saveMap(mapManagementEntity);
        return mapId;
    }

    @Override
    public Map<String, Object> getById(String id) {
        MapManagementEntity mapManagementEntity = mapManagementRepository.getById(id);
//        AttachmentInfoEntity attach = attachmentInfoService.getAttach(mapManagementEntity.getAttachId());
//        Map<String, Object> map = new HashMap<>();
//        map.put("mapManagementEntity", mapManagementEntity);
//        map.put("attach", attach);
        String attachId = mapManagementEntity.getAttachId();
        String[] attahIds = attachId.split(",");//其中建筑物综合抗震能力评价图（区县、乡镇、格网）每种类型按不同烈度6-10保存多张图，附件ID用,分隔
        List<AttachmentInfoEntity> attachList = new ArrayList<>();
        for(int i=0;i<attahIds.length;i++){
            AttachmentInfoEntity attach = attachmentInfoService.getAttach(attahIds[i]);
            attachList.add(attach);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("mapManagementEntity", mapManagementEntity);
        map.put("attachList", attachList);
        return map;
    }

    @Override
    public void deleteByIds(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        mapManagementRepository.deleteByIds(ids, timestamp);
    }

    @Override
    public MapManagementEntity update(MapManagementEntity mapManagementEntity) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        String userId = PlatformSessionUtils.getUserId();
        mapManagementEntity.setUpdateTime(timestamp);
        mapManagementEntity.setUploadTime(timestamp);
        mapManagementEntity.setUpdateUser(userId);
        if (mapManagementEntity.getUploader() == null) {
            String userJson = sUserService.getSUser(userId);
            SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
            if (sUser != null) {
                mapManagementEntity.setUploader(sUser.getUserName());
            }
        }
        if (PlatformObjectUtils.isEmpty(mapManagementEntity.getDelFlag())) {
            mapManagementEntity.setDelFlag(YNEnum.N.toString());
        }
        return mapManagementRepository.update(mapManagementEntity);
    }

    @Override
    public MapManagementEntity getByLabel(String xsemId, String mapLabel) {
        return mapManagementRepository.getByLabel(xsemId, mapLabel);
    }
}
